package com.org.javabody.Split;

import java.util.regex.Pattern;
/**
 * split的常用用法
 * @author Administrator
 *
 */
public class String_Split {
    /**
     * @param 构造函数初始化直接加载
     * @author Administrator
     */
	public String_Split() {
		String str = "a d, m, i.n";
		String limited = "\\s+|,\\s*|\\.\\s*";
		String[] arr= str.split(limited);
		for(int i = 0; i <arr.length; i++) {
			System.out.print(arr[i]);
		}
		System.out.println("");
	}
    /**
     * @param 使用预编译比单独用split更高效
     */
	public void split() {
		String regex = ",";
		String str = "Hello cat,Where are you?,I miss you!";
		Pattern p = Pattern.compile(regex);//预编译Pattern

		String[] s = p.split(str);
		for(int i = 0; i <s.length; i++) {
			System.out.println(s[i]);
		}			
	}
	/**
	 * @param \\转义字符\\\\，|转义字符\\|，其他要转义的同理
	 */
	public void split_1() {
		String str = "a\\b|c*d.e";
		String[] s = str.split("\\\\|\\*|\\||\\."); 
		for(int i = 0; i <s.length; i++) {
			System.out.print(s[i]);
		}	
		System.out.println("");
	}
	/**
	 * @param 遇到数字分割
	 */
	public void split_2() {
		String str="asdf1sdjf2jdkf3jkfd"; 
		String[] s = str.split("[0-9]");
		for(int i = 0; i <s.length; i++) {
			System.out.print(s[i]);
		}			
		System.out.println("");
	}
	
	/**
	 * @param 遇到字母分割
	 */
	public void split_3() {
		String str="asdf1sdjf2jdkf3jkfd"; 
		String[] s = str.split("[a-z]");
		for(int i = 0; i <s.length; i++) {
			System.out.print(s[i]);
		}	
		System.out.println("");
	}
	
	public static void main(String[] args) {
		String_Split ss = new String_Split();
        ss.split();
        ss.split_1();
        ss.split_2();
        ss.split_3();
	}
}
